<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Weixin\Menus;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Overtrue\Wechat\Menu;
use Overtrue\Wechat\MenuItem;
use Log;


class MenuController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        //
        $data = [];
        $data['menuList'] = Menus::getMenuList();

        if(is_pjax()) {
            return View('admin.menulistsegment', $data);
        }
        //dd(\Auth::user());
        return View('admin.menulist', $data);
    }

    /**
     * 新建菜单表单页
     */
    public function newMenu() {
        $data = [];
        $data['edit_mode'] = 0;
        $data['headmenus'] = Menus::getHeadMenu();
        if(is_pjax()) {
            return View('admin.newmenusegment', $data);
        }
        return View('admin.newmenu', $data);

    }

    /**
     * 生成菜单
     * @param Menu $mymenu
     * @return mixed
     */
    public function makeMenu(Menu $mymenu) {
        $menuList = Menus::getMenuList();
        foreach ($menuList as $menu) {
            if($menu->belongto ==0 ) {

                switch($menu->menutype) {
                    case 1:
                        $menuType = null;
                        break;
                    case 2:
                        $menuType = "click";
                        break;
                    case 3:
                        $menuType = "view";
                        break;
                }
                // 创建一个菜单项
                $item = new MenuItem($menu->menuname, $menuType, null);
                $buttons = [];
                foreach ($menuList as $menu2) {
                    if($menu2->belongto == $menu->id) {
                        switch($menu2->menutype) {
                            case 1:
                                $menuType = null;
                                break;
                            case 2:
                                $menuType = "click";
                                break;
                            case 3:
                                $menuType = "view";
                                break;
                        }
                        // 子菜单

                        $buttons[] = new MenuItem($menu2->menuname, $menuType, $menu2->menukeyword);
                    }
                }
                $item->buttons($buttons);
                $target[] = $item;
            }
        }

//        $menu_json = '[
//                        {
//                            "name": "宜精彩",
//                            "type": null,
//                            "key": null,
//                            "buttons":[
//                                {
//                                    "name":"小宜之旅",
//                                    "type":"click",
//                                    "key" :"YY_XYZL"
//                                },
//                                {
//                                    "name":"宜club",
//                                    "type":"click",
//                                    "key" :"YY_CLUB"
//                                },
//                                {
//                                    "name":"活动预告",
//                                    "type":"view",
//                                    "key" :"https://github.com/overtrue"
//                                }
//                            ]
//                        },
//                        {
//                            "name": "行政管家",
//                            "type": null,
//                            "key": null,
//                            "buttons":[
//                                {
//                                    "name":"日常攻略",
//                                    "type":"click",
//                                    "key" :"YY_RCGL"
//                                },
//                                {
//                                    "name":"相关平台登录指南",
//                                    "type":"click",
//                                    "key" :"YY_DLZN"
//                                },
//                                {
//                                    "name":"培训课程一览",
//                                    "type":"view",
//                                    "key" :"http://weibo.com/44294631"
//                                }
//                            ]
//                        },
//                        {
//                            "name": "小助手",
//                            "type": null,
//                            "key": null,
//                            "buttons":[
//                                {
//                                    "name":"个人编码查询",
//                                    "type":"view",
//                                    "key" :"http://neiwang.creditease.corp/fsm_cms3/tools/queryUserCode"
//                                },
//                                {
//                                    "name":"会议室预订",
//                                    "type":"view",
//                                    "key" :"http://http://meeting.yixinonline.org/index"
//                                },
//                                {
//                                    "name":"加班餐预订",
//                                    "type":"view",
//                                    "key" :"http://meican.com/corps/yirendai"
//                                },
//                                {
//                                    "name":"图书借阅",
//                                    "type":"view",
//                                    "key" :"http://book.yixinonline.org"
//                                },
//                                {
//                                    "name":"吐槽区",
//                                    "type":"view",
//                                    "key" :"http://weixin.happyyinyin.me/tucao/show"
//                                }
//
//                            ]
//                        }
//                    ]';
//        $menus = json_decode($menu_json);
//        $target = [];
//
//        // 构建你的菜单
//        foreach ($menus as $menu) {
//            // 创建一个菜单项
//            $item = new MenuItem($menu->name, $menu->type, $menu->key);
//
//            // 子菜单
//            if (!empty($menu->buttons)) {
//                $buttons = [];
//                foreach ($menu->buttons as $button) {
//                    $buttons[] = new MenuItem($button->name, $button->type, $button->key);
//                }
//
//                $item->buttons($buttons);
//            }
//
//            $target[] = $item;
//        }

        $mymenu->set($target); // 失败会抛出异常
        Log::info('菜单生成成功');
        return response()->json(['code'=>2000, 'message'=> '生成添加成功！']);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        //
        $menuName = $request->get('menuname');
        $menuValue = $request->get('menuvalue');
        $belongTo = $request->get('belongto');
        $menuType = $request->get('menutype');
        $menuKeyWord = $request->get('menukeyword');

        if( empty($menuName) || empty($menuType) || empty($menuValue) ) {
            return response()->json(['code'=>2001, 'message'=>'各项再检查下是不是有遗漏呀~']);
        }else {

            $form_data = [
                'menuname' => $menuName,
                'menuValue' => $menuValue,
                'menuType' => $menuType,
                'belongTo' => $belongTo,
                'menuKeyWord' => $menuKeyWord,
                'created_at' => \Carbon\Carbon::now(),
                'updated_at' => \Carbon\Carbon::now()
            ];
            $menuid = Menus::addMenu($form_data);
            if($menuid) {
                return response()->json(['code'=>2000, 'message'=> '菜单添加成功！']);
            }else {
                return response()->json(['code'=>2002, 'message'=>'菜单添加失败']);
            }
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
        $data = [];
        $data['edit_mode'] = 1;
        $data['id'] = $id;

        $data['menuinfo'] = Menus::getMenuDetail($id);
        $data['headmenus'] = Menus::getHeadMenu();
        if(is_pjax()) {
            return View('admin.newmenusegment', $data);
        }
        return View('admin.newmenu', $data);

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //
        $menuName = $request->get('menuname');
        $menuValue = $request->get('menuvalue');
        $belongTo = $request->get('belongto');
        $menuType = $request->get('menutype');
        $menuKeyWord = $request->get('menukeyword');

        if( empty($menuName) || empty($menuType) || empty($menuValue) ) {
            return response()->json(['code'=>2001, 'message'=>'各项再检查下是不是有遗漏呀~']);
        }else {

            $form_data = [
                'menuname' => $menuName,
                'menuValue' => $menuValue,
                'menuType' => $menuType,
                'belongTo' => $belongTo,
                'menuKeyWord' => $menuKeyWord,
                'updated_at' => \Carbon\Carbon::now()
            ];
            $result = Menus::updateMenu($form_data, $id);
            if($result) {
                return response()->json(['code'=>2000, 'message'=> '菜单添加成功！']);
            }else {
                return response()->json(['code'=>2002, 'message'=>'菜单添加失败']);
            }
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //
        $menuid = \Input::get('menuid');
        $result = Menus::deleteMenu($menuid);
        if($result) {
            return response()->json(['code'=>2000, 'message'=> '删除菜单成功！']);
        }else {
            return response()->json(['code'=>2002, 'message'=>'删除菜单失败！']);
        }
    }
}
